package com.dji.sample.manage.controller;

import com.dji.sample.component.mqtt.config.MqttInboundConfiguration;
import com.dji.sample.component.mqtt.model.ChannelName;
import com.dji.sample.component.mqtt.model.CommonTopicReceiver;
import com.dji.sample.component.mqtt.model.CommonTopicResponse;
import com.dji.sample.component.mqtt.service.IMqttMessageGateway;
import com.dji.sample.component.websocket.model.BizCodeEnum;
import com.dji.sample.component.websocket.model.CustomWebSocketMessage;
import com.dji.sample.component.websocket.service.ISendMessageService;
import com.dji.sample.component.websocket.service.IWebSocketManageService;
import com.dji.sample.manage.model.dto.TelemetryDTO;
import com.dji.sample.manage.model.receiver.StatusGatewayReceiver;
import com.dji.sample.manage.service.DevicePlayLineInfoService;
import com.dji.sample.map.model.dto.WebSocketElementDelDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;

/**
 * @author yjt
 * @date 2022-09-05
 * @description:
 */
@RestController
@RequestMapping("/test/mqtt/")
public class MqttController {

    @Autowired(required = false)
    IMqttMessageGateway mqttGateway;

    @Autowired
    DevicePlayLineInfoService devicePlayLineInfoService;

    @Autowired
    private ISendMessageService sendMessageService;

    @Autowired
    private IWebSocketManageService webSocketManageService;


    @RequestMapping("send")
    public String sendMsg(){
      //  mqttGateway.publish(topic,msg.getBytes(StandardCharsets.UTF_8));

        sendMessageService.sendBatch(Integer.MAX_VALUE,
                webSocketManageService.getValueWithWorkspace("xxxx"),
                CustomWebSocketMessage.<WebSocketElementDelDTO>builder()
                        .timestamp(System.currentTimeMillis())
                        .bizCode(BizCodeEnum.MAP_ELEMENT_DELETE.getCode())
                        .data(new WebSocketElementDelDTO())
                        .build());
        return "success";
    }

    @ServiceActivator(inputChannel = "testTopic")
    public void deviceOnline(Message<?> message) {
          System.out.println(message.getPayload());
    }

    @RequestMapping("save")
    public String saveTest(){
        devicePlayLineInfoService.save("1ZNDH3L0011784");
        return "success";
    }

    @RequestMapping("update")
    public String update(){
        devicePlayLineInfoService.updatePosition("1ZNDH3L0011784");
        return "success";
    }

}
